home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Magnum One
/
Magnum One (Mid-American Digital) (Disc Manufacturing).iso
/
d12
/
cgazv5n5.arc
/
LZW-TEST.C
< prev
next >
Wrap
C/C++ Source or Header
|
1991-09-23
|
5KB
|
164 lines
/*--- LZW-TEST.C ------------------------- Listing 5 ----
* Contents: This is the driver for the
* the LZW data compression program. It
* contains the following routines:
* main
* print_string_table
*
* Author: Dwayne Phillips
* Compiler: Microsoft C 6.0a, BC++ 2.0
* Note: Must compile with at least an 8K stack
* Switches: DEBUG - if defined debugging data is shown
* Date: February 1991
* May be used freely is authorship is acknowledged
*-------------------------------------------------------*/
#include "lzw.h"
#if defined(__TURBOC__)
#include <dos.h> /* for stack size control */
#endif
/*------------------------------------------------------
* Debug routine
*----------------------------------------------------*/
void print_string_table( table_item *string_table,
char *pre, int lo, int hi )
{
int s;
for ( s = lo; s <= hi; s++ ) {
printf ( "\n %s> table[%d] = ", pre, s );
if ( string_table[s].num <= BASE_TABLE &&
isprint ( string_table[s].num ))
printf( "'%c' ", string_table[s].num );
else
printf ( "%3d ", string_table[s].num );
printf ( isprint(string_table[s].character ) ?
"'%c' " : "%2dd ", string_table[s].character );
}
} /* ends print_string_table */
/*-------------------------------------------------------
* main()
*-----------------------------------------------------*/
table_item string_table[TABLE_SIZE];
void main ( int argc, char **argv )
{
char input_file_name[80],
output_file_name[80],
response[80];
FILE *infile, *outfile;
#if defined(__TURBOC__)
_stklen = 0x4000;
#endif
if ( argc >= 2 )
strcpy( response, argv[1] );
else
response[0] = 0;
if ( argc >= 3 )
strcpy ( input_file_name, argv[2] );
else
input_file_name[0] = 0;
if ( argc >=4 )
strcpy ( output_file_name, argv[3] );
else
output_file_name[0] = 0;
if ( strlen ( response ) == 0 ) {
printf ( "\n\nData compression using LZW Coding"
"\n Do you want to:"
"\n c) compress a file"
"\n d) decompress a file"
"\n q) quit\n"
"\n (c, d, q) ___\b\b" );
gets ( response );
}
if ( tolower ( response[0]) == 'q') {
printf( "\ngoodbye" );
exit ( 1 );
}
/*------------------------
* Decompression Case
*----------------------*/
if ( tolower ( response[0] ) == 'd' ) {
if ( strlen ( input_file_name ) == 0 ) {
printf("\n\nName of the file to decompress:\n\t--");
gets ( input_file_name );
}
if ( strlen ( output_file_name ) == 0 ) {
printf ( "\n\nName of the output file:\n\t--" );
gets ( output_file_name );
}
/* open file for read only */
if (( infile = fopen (input_file_name, "r+b" )) == NULL )
{
printf ( "Can't open %s.\n", input_file_name );
exit ( 1 );
}
fseek ( infile, 0L, SEEK_END );
if ( ftell ( infile ) < 2 ){
printf ( "\nERROR - input file is empty" );
exit ( 0 );
}
(void) fseek ( infile, 0L, SEEK_SET );
/* create the output file */
if (( outfile = fopen (output_file_name, "w+b" ))
== NULL) {
printf ( "Can't open %s.\n", output_file_name );
exit ( 1 );
}
decompression_routine ( string_table, infile, outfile );
fclose ( infile );
fclose ( outfile );
}
/*----------------------
Compression Case
-----------------------*/
if( tolower ( response[0]) == 'c') {
if ( strlen ( input_file_name ) == 0) {
printf (
"\n\nName of the file to be compressed:\n\t--" );
gets ( input_file_name );
}
if ( strlen ( output_file_name ) == 0 ) {
printf( "\n\nName of the output file:\n\t--" );
gets ( output_file_name );
}
/* open file for read only */
if (( infile = fopen ( input_file_name, "r+b"))
== NULL) {
printf ( "Can't open %s.\n", input_file_name );
exit ( 1 );
}
fseek ( infile, 0L, SEEK_END );
if ( ftell ( infile ) < 2 ) {
printf ( "\nERROR - input file is empty" );
exit ( 0 );
}
(void) fseek ( infile, 0L, SEEK_SET );
/* create the output file */
if (( outfile = fopen ( output_file_name, "w+b" ))
== NULL ) {
printf ( "Can't open %s.\n", output_file_name );
exit ( 1 );
}
compression_routine ( string_table, infile, outfile );
fclose ( infile );
fclose ( outfile );
} /* ends else compress input file to output file */
} /* ends main */